#include<stdio.h>
#include<string.h>
#include<stdarg.h>  
#include<stdlib.h>

#include <speex/speex.h>
#include <speex/speex_echo.h>
#include <speex/speex_preprocess.h>

#include"t2y_main.h" 
#include"resources.h"
#include"constants.h"
#include"server.h"
#include"client.h"

#ifdef UNITTEST
char printed_string[STR_BUF_LEN] = {0};

void print_info(const char *fmt, ...)
{
va_list ap;

memset(printed_string, 0, STR_BUF_LEN);
va_start(ap, fmt);
vsprintf(printed_string, fmt, ap);
va_end(ap);
}
#endif

boolean checkIP(char* ipstr)
{    
	return TRUE;
}      

void print_help(void)
{
	printf("Hi, this is help!\n");
}

/*Holds the state of the encoder*/
void *state_encode;
SpeexBits bits_encode;

/*Holds the state of the encoder*/
void *state_decode;
SpeexBits bits_decode;

#if SPEEX_ENABLE
FILE *fp_mic;
FILE *fp_play;
int tmp;
#endif

#if SPEEXDSP_ENABLE
#define NN     SIZE_AUDIO_FRAME
#define TAIL   SIZE_AUDIO_FRAME*100
SpeexEchoState *st;
SpeexPreprocessState *den;
int sampleRate = 8000;
#endif

#ifdef UNITTEST
void t2y_main(int argc, char* argv[])
#else
void main(int argc, char* argv[])
#endif
{
#if SPEEXDSP_ENABLE
   st = speex_echo_state_init(NN, TAIL);
   den = speex_preprocess_state_init(NN, sampleRate);
   speex_echo_ctl(st, SPEEX_ECHO_SET_SAMPLING_RATE, &sampleRate);
   speex_preprocess_ctl(den, SPEEX_PREPROCESS_SET_ECHO_STATE, st);
   print_info("AC enabled\n");
#endif

#if SPEEX_ENABLE
	fp_mic = fopen("mic.pcm", "wb");
	fp_play = fopen("speaker.pcm", "wb");
   /*Create a new encoder state in narrowband mode*/
   state_encode = speex_encoder_init(&speex_nb_mode);

      /*Set the quality to 8 (15 kbps)*/
   tmp=8;
   speex_encoder_ctl(state_encode, SPEEX_SET_QUALITY, &tmp);

   /*Initialization of the structure that holds the bits*/
   speex_bits_init(&bits_encode);



   /*Create a new decoder state in narrowband mode*/
   state_decode = speex_decoder_init(&speex_nb_mode);

   /*Set the perceptual enhancement on*/
   tmp=1;
   speex_decoder_ctl(state_decode, SPEEX_SET_ENH, &tmp);

   /*Initialization of the structure that holds the bits*/
   speex_bits_init(&bits_decode);

   print_info("speex acodec enabled \n");
#elif G711_ENABLED
   print_info("G711 acodec enabled\n");
#else
   print_info("no acodec enabled\n");
#endif

	if( argc == 1 || (argc == 2 && strcmp( argv[1], STR_DASH_SERVER)==0))
	{
		print_info(STR_SERVER_MODE);
#ifndef UNITTEST
		server_startup();
#endif
	}
	else if(argc == 3 && strcmp( argv[1], STR_DASH_CONNECT)==0)
	{ 
		print_info("%s%s\n", STR_CLIENT_MODE, argv[2]);
#ifndef UNITTEST
		client_startup(argv[2]);
#endif 
	}
	else if(argc == 2 
		    && (strcmp( argv[1], STR_DASH_QUESTION_MARK)==0
		          || strcmp( argv[1], STR_DASH_H)==0
		          || strcmp( argv[1], STR_DASH_HELP)==0))
	{ 
		print_help();
	}
	else
	{
		printf(STR_INVALID_PARAM);
		print_help();	
	}

}

























